﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using Wyn.PMS.Common.Utils;
using Wyn.PMS.Entity.IDataAccess;
using Wyn.PMS.Logic.Business;
using Microsoft.Practices.Unity;
using Wyn.PMS.Logic.IBusiness;

namespace Wyn.PMS.Test.Cache
{
    class Program
    {
        private static string _hotelId;
        private static string _hotelName;

        private static IUnityContainer container = UnityContainerService.GetContainer();

        static void Main(string[] args)
        {
            IHotelRepository hotelRepository = container.Resolve<IHotelRepository>();
            IUserService userService = container.Resolve<IUserService>();

            ApplicationConsole.StartCenterCache();

            string code = "Y";

            string readCode=code;

            Console.Write("-----------------------------------------------------------------\n\r");

            Console.Write("    欢迎使用酒店管理系统测试与支持控制中心 \n\r");
            Console.Write("    Welcome to new pms test and support console \n\r");
            Console.Write("    contact email : cnalexi@gmail.com \n\r");

            Console.Write("-----------------------------------------------------------------\n\r\n\r");

            while (true)
            {
                Console.Write("请输入要进行操作的酒店编号或酒店名，例如 1000811 、华沙:");

                string tempHotelFlag = Console.ReadLine();

                if (!string.IsNullOrWhiteSpace(tempHotelFlag))
                {
                    var hotel = hotelRepository.Find(GlobalConst.ROOT_HOTEL_ID, h =>h.HotelID==tempHotelFlag || h.HotelFullName.Contains(tempHotelFlag));

                    if (hotel != null)
                    {
                        _hotelId = hotel.HotelID;
                        _hotelName = hotel.HotelFullName;
                        Console.Write("您将操作：{0}，编号：{1},是否确认？（Y/N）：", hotel.HotelFullName, _hotelId);
                        var confirm = Console.ReadKey().KeyChar.ToString().ToUpper();
                        if (confirm.ToString().Trim() == code)
                        {
                            break;
                        }
                        Console.WriteLine();
                        
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("您输入的酒店编号或酒店名没查找到，请检查，谢谢。");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }
            }

            while (true)
            {
                Console.Write("\r\n账号(UserName):");
                string userName = Console.ReadLine();
                Console.Write("口令(Password):");
                string password = InputPassword();

                var loginInfo=userService.UserLoginCheck(userName, password, _hotelId, "127.0.0.1", "XXX-XXX-XXX-XXX");

                if (loginInfo.LoginState == 0)
                {
                    Console.WriteLine("恭喜您，登录成功，请继续操作。");
                    Thread.Sleep(600);
                    break;
                }

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("登录失败，请检查用户名/密码/数据库。");
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            while(readCode==code)
            {
                Console.BackgroundColor = ConsoleColor.Blue;

                Console.ForegroundColor = ConsoleColor.Gray;

                Console.Clear();

                Monitor();

                Console.ForegroundColor = ConsoleColor.Green;

                Console.Write("\n\r ^_^,返回继续进行其他测试吗?(Y/N):");

                readCode = Console.ReadKey().KeyChar.ToString().ToUpper();
            }

            Console.ForegroundColor = ConsoleColor.Red;

            Console.Write("\n\r\n\r ^_^,欢迎下次使用，再见！\n\r\n\r");

            Thread.Sleep(300);

        }
        
        static void Monitor()
        {
            ShowKeyCodeDescription();

            string key = Console.ReadKey().KeyChar.ToString().ToUpper();

            Console.WriteLine("\n\r---------您选择了测试项：{0}---------", key);

            switch (key)
            {
                case KeyCode.RoomStatusView:
                    RoomStatusViewTest.Test(_hotelId);
                    break;
                case KeyCode.RoomCountView:
                    RoomCountCacheTest.Test(_hotelId);
                    break;
                case KeyCode.RealTimeState:
                    RealTimeStateTest.Test(_hotelId);
                    break;
                case KeyCode.BizDayCache:
                    HotelBizDayCacheTest.Test();
                    break;
                case KeyCode.PriceRateCodeCache:
                    PriceCacheTest.Test(_hotelId);
                    break;
                case KeyCode.BillNo:
                    BillNoTest.Test();
                    break;
                case KeyCode.LogException:
                    LogExceptionTest.Test();
                    break;
                case KeyCode.RoomSetOOOTest:
                    YuanZhiMing yzm = new YuanZhiMing(_hotelId);
                    yzm.SetOOOTest();
                    break;
                case KeyCode.RoomCancelOOOTest:
                    YuanZhiMing yzm2 = new YuanZhiMing(_hotelId);
                    yzm2.CancelOOOTest();
                    break;
                case KeyCode.RecuseNightAudit:
                    RecuseNightAudit rna = new RecuseNightAudit(_hotelId, _hotelName);
                    rna.Recuse();
                    break;
                case KeyCode.SearchInvoice:
                    var si = new SearchInvoice(_hotelId);
                    si.Search();
                    break;
                default:
                    OtherCommonTest.Test();
                    break;
            }

            Console.WriteLine("\n\r---------测试完成---------");
        }
        
        static void ShowKeyCodeDescription()
        {
            StringBuilder desc = new StringBuilder();

            desc.Append("-----------------------------------------------------------\n\r");

            desc.Append("            欢迎来到新系统测试与支持中心 \n\r");
            desc.Append(string.Format("            您当前操作的是:{0} \n\r",_hotelName));
            desc.Append("    Welcome to new pms test and support center \n\r");

            desc.Append("-----------------------------------------------------------\n\r    ");

            desc.Append(string.Format("{0}. 房态缓存测试 \n\r    ", KeyCode.RoomStatusView));

            desc.Append(string.Format("{0}. 房量缓存测试 \n\r    ", KeyCode.RoomCountView));

            desc.Append(string.Format("{0}. 实时运营状态测试 \n\r    ", KeyCode.RealTimeState));

            desc.Append(string.Format("{0}. 营业日测试 \n\r    ", KeyCode.BizDayCache));

            desc.Append(string.Format("{0}. 房价码测试 \n\r    ", KeyCode.PriceRateCodeCache));

            desc.Append(string.Format("{0}. 生成账单测试 \n\r    ", KeyCode.BillNo));

            desc.Append(string.Format("{0}. 日志以及抓取异常测试 \n\r    ", KeyCode.LogException));

            desc.Append(string.Format("{0}. 设置豁免房测试 \n\r    ", KeyCode.RoomSetOOOTest));

            desc.Append(string.Format("{0}. 取消豁免房测试 \n\r    ", KeyCode.RoomCancelOOOTest));

            desc.Append(string.Format("{0}. 重新调整夜审报表315,318,319数据 \n\r    ", KeyCode.RecuseNightAudit));

            desc.Append(string.Format("{0}. 查询发票 \n\r    ", KeyCode.SearchInvoice));

            desc.Append(string.Format("X. 其他日常测试 \n\r    "));
            
            desc.Append("\n\r^_^,请输入相应代码进行测试:");

            Console.Write(desc.ToString());

        }

        static string InputPassword()
        {
            StringBuilder sb = new StringBuilder();

            while (true)
            {
                ConsoleKeyInfo cki = Console.ReadKey(true);

                if (cki.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine();
                    break;
                }

                if (cki.Key == ConsoleKey.Backspace)
                {
                    if (sb.Length > 0)
                    {
                        Console.Write("\b\0\b");
                        sb.Length--;
                    }
                    continue;
                }
                Console.Write("*");
                sb.Append(cki.KeyChar);
            }
            return sb.ToString();
        }

        class KeyCode
        {
            //房态
            public const string RoomStatusView = "1";
            public const string RoomCountView = "2";
            public const string RealTimeState = "3";
            public const string BizDayCache = "4";
            public const string PriceRateCodeCache = "5";
            public const string BillNo = "8";
            public const string LogException = "9";
            public const string RoomSetOOOTest = "Z";
            public const string RoomCancelOOOTest = "C";
            public const string RecuseNightAudit = "T";
            public const string SearchInvoice = "V";

        }
        
        
    }
}
